function refresh(){
    $("#dataTable").bootstrapTable("removeAll");
    $("#dataTable").bootstrapTable('refresh');
}
//搜索以前将页码换成第一页
function searchBeforeInit(){
    var options=$("#dataTable").bootstrapTable("getOptions");
    options.pageNumber=1;
    $("#dataTable").bootstrapTable('refreshOptions',options);
}
function initSearchEvent(){

    $("#serachBtn").click(function(){
        searchBeforeInit();
        refresh();
    });
    $('#serachBtn').bind('keypress',function(e){


        if(e.keyCode == 13){
            searchBeforeInit();
            refresh();
        }
    });
    $('#taskCode').bind('keypress',function(e){

        if(e.keyCode == 13){
            searchBeforeInit();
            refresh();
        }
    });
    $('#taskName').bind('keypress',function(e){

        if(e.keyCode == 13){
            searchBeforeInit();
            refresh();
        }
    });
    $("#searchReset").on("click",function(){
        //$("#searchForm")[0].reset();
        $("#searchForm input[type='text']").val("");
    });

}
function editData(rowId){
    $$.goUrl('/iot/task/task_update.html?rowId='+rowId);
    /*
    layer.open({
        move: false,
        type: 2,
        title: '新建',
        area: ['800px', '420px'],
        content: 'task_update.html?rowId='+rowId,
        btn: ['保存','取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
            var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var ret= openWin.yesOperator();
            if(ret){
                layer.close(index);
                refresh();
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
    */
}
function showSubDatas(rowId,taskCode){
    $$.goUrl('/iot/task/task_data_list.html?taskId='+rowId+'&taskCode='+taskCode);

}
function deleteData(){
    layer.confirm('确定要删除这条数据吗？',
        {icon: 3, title: '提示', area: '300px', btnAlign: 'c'}, function (index) {
            var rows= $("#dataTable").bootstrapTable('getSelections');
            var rowIds = [];
            for (var i = 0; i < rows.length; i++) {
                rowIds.push(rows[i].id);
            }
            $$.ajax({
                url: "/rest/IotTaskBusiness/remove",
                data: {
                    id : rowIds.join(',')
                },
                success: function() {
                    refresh();
                }
            });
            layer.close(index);
        });
}
function addData(){
    layer.open({
        move: false,
        type: 2,
        title: '新建',
        area: ['800px', '420px'],
        content: 'task_add.html',
        btn: ['保存','取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
            var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var ret= openWin.yesOperator();
            if(ret){
                layer.close(index);
                refresh();
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
}



$(function () {
    initSearchEvent();
    $('#taskCode').focus();
    //重置表单
    $("#searchReset").on("click",function(){
        $('#taskCode').val('');
        $('#taskName').val('');
        $('#dataTable').bootstrapTable('refresh');
    });

    $("#del-btn").click(function(){
        var len = $("#dataTable input:checked").length;
        if (len == 0) {
            //未选中数据提示框
            layer.msg('请选择数据',  {time:1000});
        } else {
            deleteData();
        }
    });

    //渲染表格
    var columns = taskList.columnsInit();
    var table = taskList.gridInit(columns);
    //var datas = taskList.dataInit();
    //$("#dataTable").bootstrapTable("append",datas);

})
;(function(undefined) {
    "use strict"
    var _global;

    function result(args,fn){
        var argsArr = Array.prototype.slice.call(args);
        if(argsArr.length > 0){
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }
    var taskList = {
        columnsInit:function(){
            return  [{
                field: 'id',
                title: '序号',
                width:'2%',
                align:'center',
                valign: 'middle',
                formatter: function (value, row, index) {
                    return index + 1;
                }
            },
                {field:"taskCode",title:"任务编码" },
                {field:"taskName",title:"任务名称" },
                {field:"taskPeriod",title:"任务周期" },
                {field:"taskHs",title:"任务执行时分秒" },
                {field:"taskUnit",title:"任务单位" ,
                    formatter: function (value, row, index) {
                        if(value == '1'){
                            return [
                                '日'
                            ];
                        }else if(value == '2'){
                            return [
                                '月'
                            ];
                        }else if(value == '3'){
                            return [
                                '年'
                            ];
                        }else{
                            return "-";
                        }

                    }
                },
                {field:"taskExpect",title:"任务预期执行时间" },
                {field:"taskGroup",title:"任务组" },
                {field:"taskMain",title:"是否主任务" ,
                    formatter: function (value, row, index) {
                        if(value == '1'){
                            return [
                                '主任务'
                            ];
                        }else if(value == '2'){
                            return [
                                '子任务'
                            ];
                        }
                    }},
                {field:"iotTable",title:"存储表格" },
                {field:"iotTime",title:"采样时间" },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        if(value == '1'){
                            return [
                                '<span class="sys-type-bg pro-status-green">启用</span>'
                            ];
                        }
                        return [
                            '<span class="sys-type-bg pro-status-red">停用</span>'
                        ];

                    }

                },
                {
                    field: 'optionDel',
                    checkbox: true
                },{
                    field: 'option',
                    title: '操作',
                    width: 80,
                    align: 'center',
                    formatter: function (value, row, index) {
                        /*//up菜单向上展示、 down向下展示
                        var pageSize = deviceList.pageSize;
                        var cssStyle = 'bottom';
                        if (index > pageSize / 3) {
                            cssStyle = 'top';
                        }*/
                        return [
                            '<div class="opt-dropdown tb-opt-div"><a class="tb-opt-icon" href="javascript:void(0)">操作 <i class="glyphicon glyphicon-triangle-bottom"></i></a>',
                            '<ul class="opt-dropdown-menu opt-dropdown-menu-bottom">',
                            '<li><a href="javascript:editData(\''+row.id+'\')" title="编辑">',
                            '<i class="glyphicon glyphicon-pencil"></i><em> 编辑</em></a></li>',
                            '<li><a href="javascript:showSubDatas(\''+row.id+'\',\''+row.taskCode+'\')" title="数据">',
                            '<i class="glyphicon glyphicon-th-list"></i><em> 数据</em></a></li>',
                            '<li><a href="javascript:getLog(\''+row.id+'\',\''+row.deviceCode+'\')"><i class="glyphicon glyphicon-cloud-download"></i><em> 获取日志</em></a></li></ul></div>'
                        ].join('');
                    }

                }]
        },
        gridInit:function(columns){
            return $("#dataTable").bootstrapTable({
                method:"post",
                contentType:"application/x-www-form-urlencoded",
                url:$$.restServerUrl+'/bootstrap/basegrid/IotTaskBusiness/list',
                columns: columns,
                undefinedText:'-',
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                clickToSelect: true,
                singleSelect:false,
                sidePagination: "server",
                queryParamsType : "page",
                sortSide:'client',
                sortable:true,
                pageNumber: 1,
                pageSize:10,
                idField:'id',
                silentSort: true,
                pageList: [
                	10, 20, 50, 100
                ],
                onClickRow:function(row, $element,field){
                    $("#dataTable").bootstrapTable("uncheckAll");
                },
                queryParams: function queryParams(params) {   //设置查询参数

                    var args={};
                    args.c_taskCode_7=$("#taskCode").val();
                    args.c_taskName_7=$("#taskName").val();
                    args.page=params.pageNumber-1; //    params.pageNumber
                    args.pageSize=params.pageSize;     //params.pageSize
                    args.isPage=1;
                    //args.order=orderStr;
                    var paramStr=JSON.stringify(args);

                    //alert(params.pageNumber+":"+params.pageSize);
                    //alert(orderStr);
                    var param = {
                        params:paramStr
                    };
                    return param;
                },
                onPostBody:function () {
                    $(".opt-dropdown").hover(function(){
                        $(this).find(".opt-dropdown-menu").show();
                    },function(){
                        $(this).find(".opt-dropdown-menu").hide();
                    })
                }
            })

        },
        dataInit:function(){
            return  []
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = taskList;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return taskList;});
    } else {
        !('plugin' in _global) && (_global.taskList = taskList);
    }
}());